﻿using System;
using System.Web;
using System.Web.UI;
using UDS.Components;

namespace UDS.SubModule.UnitiveDocument
{
    /// <summary>
    /// ProjectDetail 的摘要说明。
    /// </summary>
    public partial class ProjectDetailPage : Page
    {
        private string Action = "";
        // private bool bSubscription;
        private string Username;
        protected int classID;

        protected override void OnLoad(EventArgs e)
        {
            if (Request.QueryString["classID"] != null)
            {
                classID = Int32.Parse(Request.QueryString["classID"]);
            }
            Action = (Request.QueryString["Action"] != null) ? Request.QueryString["Action"] : "";
            if (Action == "5")
            {
                SubscribeProject();
            }

            Username = SiteContext.Current.UserName;
            //if (Request.QueryString["Subscription"] == null)
            //{
            //    bSubscription = false;
            //}
            //else
            //{
            //    bSubscription = Int32.Parse(Request.QueryString["Subscription"]) > 0 ? true : false;
            //}

            if (!Page.IsPostBack)
            {
                PopulateData();
            }

            base.OnLoad(e);
        }

        #region 显示数据

        /// <summary>
        /// 显示数据
        /// </summary>
        private void PopulateData()
        {
            var pjt = new ProjectClass();

            #region 获取项目信息

            using (var dataReader = pjt.GetClassInfo(classID))
            {
                try
                {
                    if (dataReader.Read())
                    {
                        lblClassName.Text = dataReader[0].ToString();
                        lblParentClassName.Text = dataReader[2].ToString();
                        lblBuildDate.Text = dataReader[3].ToString();
                        lblEndDate.Text = dataReader[4].ToString();
                        lblFinishedScale.Text = dataReader[8].ToString();
                        lblDescription.Text = dataReader[7].ToString();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }

            #endregion

            #region 获取子项目信息

            using (var dataReader = pjt.GetSubClassInfo(classID))
            {
                try
                {
                    while (dataReader.Read())
                    {
                        lblSubClass.Text += "<a href='Switch.aspx?Action=1&ClassID=" + dataReader[0] + "'>" + dataReader[1] + "</a>&nbsp;&nbsp;";
                    }
                    if (lblSubClass.Text == "") lblSubClass.Text = "无";
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }

            #endregion

            #region 获取项目成员信息

            var staffList = Staff.GetStaffInTeam(classID);
            foreach (var staff in staffList)
            {
                lblMember.Text += "<a href='Mail/Compose.aspx?Action=3"
                                  + "&ClassID=" + classID
                                  + "&Username=" + staff.Name
                                  + "&Name=" + HttpUtility.HtmlAttributeEncode(staff.RealName)
                                  + "'>"
                                  + HttpUtility.HtmlEncode(staff.RealName)
                                  + "</a> &nbsp;&nbsp;";
                if (lblMember.Text == "")
                {
                    lblMember.Text = "无";
                }
            }

            #endregion

            #region 获取项目上级负责人信息

            using (var dataReader = pjt.GetParentLeader(classID))
            {
                try
                {
                    if (dataReader.Read())
                    {
                        lblParentLeader.Text += "<a href='Mail/Compose.aspx?Action=3"
                                                + "&ClassID=" + classID
                                                + "&Username=" + dataReader["Staff_Name"]
                                                + "&Name=" + Server.UrlEncode(dataReader["RealName"].ToString())
                                                + "'>"
                                                + dataReader["RealName"]
                                                + "</a> &nbsp;&nbsp;";
                    }
                    if (lblParentLeader.Text == "") lblParentLeader.Text = "无";
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }

            #endregion

            #region 获取项目负责人信息

            using (var dataReader = pjt.GetLeader(classID))
            {
                try
                {
                    while (dataReader.Read())
                    {
                        lblLeader.Text += "<a href='Mail/Compose.aspx?Action=3"
                                          + "&ClassID=" + classID
                                          + "&Username=" + dataReader["Staff_Name"]
                                          + "&Name=" + Server.UrlEncode(dataReader["RealName"].ToString())
                                          + "'>"
                                          + dataReader["RealName"]
                                          + "</a> &nbsp;&nbsp;";
                    }
                    if (lblLeader.Text == "") lblLeader.Text = "无";
                }
                catch (Exception ex)
                {
                    Logger.Log(ex.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }

            #endregion

            #region 注释

            /*			
			#region 根据权限显示相关功能标题
			// 设置缺省值
			int cstRightToApproveDocument 	= 2;
			int cstRightToViewDocument		= 10;
			int cstRightToBuildNode		 	= 5;
			int cstDisplayMember			= 6;
			int cstTeamRight				= 7;
			int cstComposeMail				= 11;
			int cstDeliveryDoc				= 11;
			int cstProjectMove				= 5;
			this.lblManageProject.Visible   = pjt.GetAccessPermission(classID,Username,cstRightToBuildNode);
			this.imgManageProject .Visible  = this.lblManageProject .Visible ;
			this.lblManageDirectory.Visible = pjt.GetAccessPermission(classID,Username,cstRightToBuildNode);
			this.imgManageDirectory .Visible= this.lblManageDirectory .Visible ;
			this.lblComposeMail.Visible     = pjt.GetAccessPermission(classID,Username,cstComposeMail);
			this.imgComposeMail .Visible    = this.lblComposeMail .Visible ;
			this.lblDeliveryDoc.Visible     = pjt.GetAccessPermission(classID,Username,cstDeliveryDoc);
			this.imgDeliveryDoc.Visible     = this.lblDeliveryDoc .Visible ;
			this.lblShowMember.Visible      = pjt.GetAccessPermission(classID,Username,cstDisplayMember);
			this.imgShowMember .Visible     = this.lblShowMember .Visible ;
			this.lblManagePermission.Visible= pjt.GetAccessPermission(classID,Username,cstTeamRight);
			this.imgManagePermission .Visible = this.lblManagePermission .Visible ;
			this.lblRemove.Visible = pjt.GetAccessPermission(classID,Username,cstProjectMove);
			this.imgRemove.Visible = this.lblRemove.Visible;
			

			#endregion
		
			#region 根据权限取出邮件数据
			/// <summary>
			/// 将某用户的取出邮件数据
			/// </summary>
			
			dataTable			  = mail.GetClassMails(classID,UserCookie.Value.ToString());
			dgMailList.DataSource = dataTable.DefaultView;
			dgMailList.DataBind(); 
			
			mail = null;
			
			#endregion

			#region 根据权限取出文档数据
			/// <summary>
			/// 将某用户的文档数据取出
			/// </summary>
			if(pjt.GetAccessPermission(classID,Username,cstRightToViewDocument)||bSubscription)
			{
				dataTable			  = doc.GetClassDocs(classID);
				dgDocList.DataSource  = dataTable.DefaultView;
				dgDocList.DataBind(); 
				dataTable = null;
			}
			else
			{
				IMG1.Visible	  = false;
				IMG4.Visible      = false;
				dgDocList.Visible = false;
			}
			#endregion

			#region 根据权限取出某项目的审批文档数据
			/// <summary>
			/// 将某项目的审批文档数据取出
			/// </summary>
			if(pjt.GetAccessPermission(classID,Username,cstRightToApproveDocument))
			{
				dataTable		         = doc.GetApproveClassDocs(classID);
				dgAppDocList.DataSource  = dataTable.DefaultView;
				dgAppDocList.DataBind(); 
				dataTable = null;
				doc		  = null;
			}
			else
			{
				IMG3.Visible		 = false;
				dgAppDocList.Visible = false;
				lblApp.Visible       = false;
			}
			#endregion
			*/

            #endregion
        }

        #endregion

        #region 订阅项目

        /// <summary>
        /// 订阅项目
        /// </summary>
        private void SubscribeProject()
        {
            var prj = new ProjectClass();
            try
            {
                prj.Subscribe(Username, classID);
                Response.Write("<script language=javascript>alert('订阅成功!');</script>");
            }
            catch (Exception ex)
            {
                Logger.Log(ex.ToString());
                Server.Transfer("../Error.aspx");
            }
        }

        #endregion
    }
}